package org.begincode.admin.control;

import com.begincode.common.constant.Constant;
import com.begincode.common.util.ResultUtil;
import org.begincode.admin.service.BlogService;
import org.begincode.core.model.Blog;
import org.begincode.core.model.BlogComment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yangsj on 2016/4/24.
 * 博文管理
 */
@Controller
@RequestMapping("blogManage")
public class BlogContrller {
    Logger logger = LoggerFactory.getLogger(BlogContrller.class);
    @Resource
    private BlogService blogService;

    /**
     * 查看未审核博文
     * */
    @ResponseBody
    @RequestMapping(value = "selUnCheckBlogs")
    public Object selUnCheckBlogs(){
        logger.info("selUnCheckBlogs");
        List<Blog> blogs =  blogService.selUnCheckBlogs();
        return ResultUtil.buildSuccess(blogs);
    }
    /**
     * 查看未审核博文评论
     * */
    @ResponseBody
    @RequestMapping(value = "selUnCheckBlogComments")
    public Object selUnCheckBlogComments(){
        logger.info("selUnCheckBlogComments");
        List<BlogComment> blogComments =  blogService.selUnCheckBlogComment();
        return ResultUtil.buildSuccess(blogComments);
    }
    /**
     * 通过博文
     * */
    @ResponseBody
    @RequestMapping(value = "passBlogs/{blogId}")
    public Object passBlogs(@PathVariable("blogId") int blogId){
        logger.info("passBlogs blogId = {}", blogId);
        Blog blog = new Blog();
        blog.setBlogId(blogId);
        blog.setCheckFlag(Constant.CHECKED);
        int updateCount =  blogService.updateBlogById(blog);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
    /**
     * 删除博文
     * */
    @ResponseBody
    @RequestMapping(value = "delBlogs/{blogId}")
    public Object delBlogs(@PathVariable("blogId") int blogId){
        logger.info("delBlogs blogId = {}", blogId);
        Blog blog = new Blog();
        blog.setBlogId(blogId);
        blog.setDeleteFlag(Constant.DEL_FLAG);
        blog.setCheckFlag(Constant.CHECKED);
        int updateCount =  blogService.updateBlogById(blog);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
    /**
     * 通过博文评论
     * */
    @ResponseBody
    @RequestMapping(value = "passBlogComment/{blogCommentId}")
    public Object passBlogComment(@PathVariable("blogCommentId") int blogCommentId){
        logger.info("passBlogComment blogCommentId = {}", blogCommentId);
        BlogComment blogComment = new BlogComment();
        blogComment.setBlogCommentId(blogCommentId);
        blogComment.setCheckFlag(Constant.CHECKED);
        int updateCount =  blogService.updateBlogCommentById(blogComment);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
    /**
     * 删除博文评论
     * */
    @ResponseBody
    @RequestMapping(value = "delBlogComment/{blogCommentId}")
    public Object delBlogComment(@PathVariable("blogCommentId") int blogCommentId){
        logger.info("delBlogComment blogCommentId = {}", blogCommentId);
        BlogComment blogComment = new BlogComment();
        blogComment.setBlogCommentId(blogCommentId);
        blogComment.setCommentStatus(Constant.DEL_FLAG);
        blogComment.setCheckFlag(Constant.CHECKED);
        int updateCount =  blogService.updateBlogCommentById(blogComment);
        if(updateCount == 1){
            return ResultUtil.buildSuccess();
        }
        return ResultUtil.buildFail();
    }
}
